# -*- coding:utf-8 -*-
from .transport import Transport

class TgwServerMixin(object):
    def __init__(self):
        self._handshake_buff = ''
        self._handshaked = False

    def recv(self):
#        print 'call tgw recv.'
        data = Transport.recv(self)
#        print 'tgw recv:', repr(data)
        if self._handshaked:
            return data
        return self._do_handshake(data)

    def _do_handshake(self, data):
        self._handshake_buff += data
        if '\r\n\r\n' not in self._handshake_buff:
            return ''
        _, remain = self._handshake_buff.split('\r\n\r\n', 1)
        self._handshaked = True
        return remain

class TgwClientMixin(object):
    def __init__(self):
        self.send(('tgw_l7_forward\r\nHost: '
            + ':'.join(map(str,
                self._ll_transport.getpeername()))
            + '\r\n\r\n'
            ))

